package cc.mrbird.card.controller;

import cc.mrbird.card.domain.CardInfo;
import cc.mrbird.card.domain.RefundCard;
import cc.mrbird.card.domain.RefundCardVo;
import cc.mrbird.card.service.CardInfoService;
import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.system.domain.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class CardInfoController extends BaseController {

    @Autowired
    private CardInfoService cardInfoService;

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("cardInfo")
    @RequiresPermissions("cardInfo:list")
    public String index(Model model) {
        User user = super.getCurrentUser();
        model.addAttribute("user", user);
        return "card/cardInfo/index";
    }

    @RequestMapping("cardInfo/refundCardIndex")
    public String refundCardIndex(Model model) {
        User user = super.getCurrentUser();
        model.addAttribute("user", user);
        return "card/cardInfo/refundCardIndex";
    }

    @Log("获取退卡登记列表")
    @RequestMapping("cardInfo/refundCardList")
    @ResponseBody
    public Map<String, Object> refundCardList(QueryRequest request, RefundCard refundCard) {
        refundCard.setCardno(StringUtils.isEmpty(refundCard.getCardno())?null:refundCard.getCardno());
        refundCard.setPaperid(StringUtils.isEmpty(refundCard.getPaperid())?null:refundCard.getPaperid());
        refundCard.setCardusername(StringUtils.isEmpty(refundCard.getCardusername())?null:refundCard.getCardusername());
        refundCard.setRefundordernum(StringUtils.isEmpty(refundCard.getRefundordernum())?null:refundCard.getRefundordernum());
        Map<String, Object> map = super.selectByPageNumSize(request, () -> this.cardInfoService.getRefundCardList(refundCard, request));
        List<RefundCard> list = (List<RefundCard>) map.get("rows");
        if(list == null || list.size()==0){
            return map;
        }
        List<RefundCardVo> voList = new ArrayList<>();
        for (RefundCard card:list){
            RefundCardVo refundCardVo = new RefundCardVo();
            refundCardVo.setId(card.getId());
            refundCardVo.setRefundordernum(card.getRefundordernum());
            refundCardVo.setCardno(card.getCardno());
            refundCardVo.setCardusername(card.getCardusername());
            refundCardVo.setPaperid(card.getPaperid());
            refundCardVo.setSyscardbanlance(BigDecimal.valueOf(card.getSyscardbanlance()).divide(new BigDecimal(100)));
            refundCardVo.setCardbanlance(BigDecimal.valueOf(card.getCardbanlance()).divide(new BigDecimal(100)));
            refundCardVo.setDeposit(BigDecimal.valueOf(card.getDeposit()).divide(new BigDecimal(100)));
            refundCardVo.setRefundamt(BigDecimal.valueOf(card.getRefundamt()).divide(new BigDecimal(100)));
            refundCardVo.setCardtypename(card.getCardtypename());
            refundCardVo.setRefunddate(card.getRefunddate());
            refundCardVo.setRefunduser(card.getRefunduser());
            refundCardVo.setRefundusername(card.getRefundusername());
            voList.add(refundCardVo);
        }
        map.put("rows",voList);
        return map;
    }


    @Log("获取卡用户信息")
    @RequestMapping("cardInfo/list")
    @RequiresPermissions("cardInfo:list")
    @ResponseBody
    public Map<String, Object> userList(QueryRequest request, CardInfo cardInfo) {
        return super.selectByPageNumSize(request, () -> this.cardInfoService.getAllList(cardInfo, request));
    }

    @Log("获取卡用户信息")
    @RequestMapping("cardInfo/getCardInfoByCardNo")
    @ResponseBody
    public ResponseBo getCardInfoByCardNo(CardInfo cardInfo){
        String cardNo = cardInfo.getCardNo();
        String cardHEX = cardNo.substring(8,16);
        String card10 = new BigInteger(cardHEX, 16).toString(10);
        Integer len = card10.length();
        while (len != 8){
            card10 = "0"+ card10;
            len++;
        }
        cardNo = cardNo.substring(0,8).concat(card10);
        cardInfo.setCardNo(cardNo);
        return ResponseBo.ok().put("cardInfo",this.cardInfoService.getAllList(cardInfo, null));
    }

    @Log("获取卡用户信息")
    @RequestMapping("cardInfo/getNotCardInfoByCardNo")
    @ResponseBody
    public ResponseBo getNotCardInfoByCardNo(CardInfo cardInfo){
        String cardNo = cardInfo.getCardNo();
        cardInfo.setCardNo(cardNo);
        return ResponseBo.ok().put("cardInfo",this.cardInfoService.getAllList(cardInfo, null));
    }



    @Log("登记退卡信息")
    @RequestMapping("cardInfo/commitRefundCard")
    @ResponseBody
    public ResponseBo commitRefundCard(CardInfo cardInfo,Double refundamt,Double deposit){
        try{
            RefundCard refundCard = cardInfoService.commitRefundCardInfo(cardInfo,getCurrentUser(),refundamt,deposit);
            return ResponseBo.ok("OK",refundCard);
        }catch (Exception ex){
            ex.printStackTrace();
            return ResponseBo.error(ex.getMessage());
        }
    }
}
